package com.buaa.divinationmaster.core.database.dao;

import androidx.lifecycle.LiveData;
import androidx.room.Dao;
import androidx.room.Delete;
import androidx.room.Insert;
import androidx.room.OnConflictStrategy;
import androidx.room.Query;
import androidx.room.Update;

import com.buaa.divinationmaster.core.database.entities.YaoText;

import java.util.List;

/**
 * 爻辞数据访问对象 (DAO)
 * 提供384条爻辞数据的查询功能，支持梅花易数和金钱卦的详细解读
 */
@Dao
public interface YaoTextDao {
    
    /**
     * 获取所有爻辞，按卦象ID和爻位排序
     * @return LiveData包装的爻辞列表
     */
    @Query("SELECT * FROM yao_texts ORDER BY hexagram_id, yao_position")
    LiveData<List<YaoText>> getAllYaoTexts();
    
    /**
     * 获取所有爻辞 (同步方法)
     * @return 爻辞列表
     */
    @Query("SELECT * FROM yao_texts ORDER BY hexagram_id, yao_position")
    List<YaoText> getAllYaoTextsSync();
    
    /**
     * 根据卦象ID获取所有相关爻辞
     * @param hexagramId 卦象ID (1-64)
     * @return 该卦象的6条爻辞
     */
    @Query("SELECT * FROM yao_texts WHERE hexagram_id = :hexagramId ORDER BY yao_position")
    List<YaoText> getYaoTextsByHexagramId(int hexagramId);
    
    /**
     * 根据卦象ID获取所有相关爻辞 (LiveData)
     * @param hexagramId 卦象ID
     * @return LiveData包装的爻辞列表
     */
    @Query("SELECT * FROM yao_texts WHERE hexagram_id = :hexagramId ORDER BY yao_position")
    LiveData<List<YaoText>> getYaoTextsByHexagramIdLive(int hexagramId);
    
    /**
     * 根据卦象ID和爻位获取特定爻辞
     * @param hexagramId 卦象ID
     * @param yaoPosition 爻位 (1-6)
     * @return 特定爻辞
     */
    @Query("SELECT * FROM yao_texts WHERE hexagram_id = :hexagramId AND yao_position = :yaoPosition")
    YaoText getYaoTextByPosition(int hexagramId, int yaoPosition);
    
    /**
     * 根据ID获取爻辞
     * @param id 爻辞ID
     * @return 爻辞对象
     */
    @Query("SELECT * FROM yao_texts WHERE id = :id")
    YaoText getYaoTextById(long id);
    
    /**
     * 根据爻性质获取爻辞 (阳爻或阴爻)
     * @param yaoType 爻性质 ("1"表示阳爻, "0"表示阴爻)
     * @return 特定性质的爻辞列表
     */
    @Query("SELECT * FROM yao_texts WHERE yao_type = :yaoType ORDER BY hexagram_id, yao_position")
    List<YaoText> getYaoTextsByType(String yaoType);
    
    /**
     * 获取所有阳爻
     * @return 阳爻列表
     */
    @Query("SELECT * FROM yao_texts WHERE yao_type = '1' ORDER BY hexagram_id, yao_position")
    List<YaoText> getYangYaoTexts();
    
    /**
     * 获取所有阴爻
     * @return 阴爻列表
     */
    @Query("SELECT * FROM yao_texts WHERE yao_type = '0' ORDER BY hexagram_id, yao_position")
    List<YaoText> getYinYaoTexts();
    
    /**
     * 根据爻位获取所有爻辞 (如获取所有初爻、二爻等)
     * @param yaoPosition 爻位 (1-6)
     * @return 特定爻位的所有爻辞
     */
    @Query("SELECT * FROM yao_texts WHERE yao_position = :yaoPosition ORDER BY hexagram_id")
    List<YaoText> getYaoTextsByPosition(int yaoPosition);
    
    /**
     * 根据五行属性获取爻辞
     * @param element 五行属性
     * @return 特定五行的爻辞列表
     */
    @Query("SELECT * FROM yao_texts WHERE element = :element ORDER BY hexagram_id, yao_position")
    List<YaoText> getYaoTextsByElement(String element);
    
    /**
     * 根据六亲获取爻辞
     * @param sixRelatives 六亲属性
     * @return 特定六亲的爻辞列表
     */
    @Query("SELECT * FROM yao_texts WHERE six_relatives = :sixRelatives ORDER BY hexagram_id, yao_position")
    List<YaoText> getYaoTextsBySixRelatives(String sixRelatives);
    
    /**
     * 根据关键词搜索爻辞
     * @param keyword 关键词
     * @return 包含关键词的爻辞列表
     */
    @Query("SELECT * FROM yao_texts WHERE keywords LIKE '%' || :keyword || '%' ORDER BY hexagram_id, yao_position")
    List<YaoText> searchByKeywords(String keyword);
    
    /**
     * 根据爻辞内容搜索
     * @param searchQuery 搜索内容
     * @return 匹配的爻辞列表
     */
    @Query("SELECT * FROM yao_texts WHERE yao_text LIKE '%' || :searchQuery || '%' OR yao_meaning LIKE '%' || :searchQuery || '%' ORDER BY hexagram_id, yao_position")
    List<YaoText> searchYaoTexts(String searchQuery);
    
    /**
     * 获取有动爻解释的爻辞
     * @return 有动爻解释的爻辞列表
     */
    @Query("SELECT * FROM yao_texts WHERE moving_interpretation IS NOT NULL AND moving_interpretation != '' ORDER BY hexagram_id, yao_position")
    List<YaoText> getMovingYaoTexts();
    
    /**
     * 根据多个卦象ID获取爻辞 (用于批量查询)
     * @param hexagramIds 卦象ID列表
     * @return 多个卦象的爻辞列表
     */
    @Query("SELECT * FROM yao_texts WHERE hexagram_id IN (:hexagramIds) ORDER BY hexagram_id, yao_position")
    List<YaoText> getYaoTextsByHexagramIds(List<Integer> hexagramIds);
    
    /**
     * 获取指定卦象的动爻解释
     * @param hexagramId 卦象ID
     * @param yaoPositions 动爻位置列表
     * @return 动爻解释列表
     */
    @Query("SELECT * FROM yao_texts WHERE hexagram_id = :hexagramId AND yao_position IN (:yaoPositions) ORDER BY yao_position")
    List<YaoText> getMovingYaosByPositions(int hexagramId, List<Integer> yaoPositions);
    
    /**
     * 插入所有爻辞 (批量插入，用于初始化数据)
     * @param yaoTexts 爻辞列表
     */
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    void insertAll(List<YaoText> yaoTexts);
    
    /**
     * 插入单条爻辞
     * @param yaoText 爻辞对象
     * @return 插入的行ID
     */
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    long insert(YaoText yaoText);
    
    /**
     * 更新爻辞信息
     * @param yaoText 爻辞对象
     */
    @Update
    void update(YaoText yaoText);
    
    /**
     * 删除爻辞
     * @param yaoText 爻辞对象
     */
    @Delete
    void delete(YaoText yaoText);
    
    /**
     * 获取爻辞总数
     * @return 总数 (应该是384条: 64卦×6爻)
     */
    @Query("SELECT COUNT(*) FROM yao_texts")
    int getYaoTextCount();
    
    /**
     * 获取特定卦象的爻辞数量
     * @param hexagramId 卦象ID
     * @return 该卦象的爻辞数量 (应该是6条)
     */
    @Query("SELECT COUNT(*) FROM yao_texts WHERE hexagram_id = :hexagramId")
    int getYaoTextCountByHexagram(int hexagramId);
    
    /**
     * 获取阳爻数量
     * @return 阳爻总数
     */
    @Query("SELECT COUNT(*) FROM yao_texts WHERE yao_type = '1'")
    int getYangYaoCount();
    
    /**
     * 获取阴爻数量
     * @return 阴爻总数
     */
    @Query("SELECT COUNT(*) FROM yao_texts WHERE yao_type = '0'")
    int getYinYaoCount();
    
    /**
     * 检查爻辞数据是否已初始化
     * @return 是否已初始化 (应该有384条记录)
     */
    @Query("SELECT COUNT(*) >= 384 FROM yao_texts")
    boolean isDataInitialized();
    
    /**
     * 获取所有不重复的五行属性
     * @return 五行属性列表
     */
    @Query("SELECT DISTINCT element FROM yao_texts WHERE element IS NOT NULL AND element != '' ORDER BY element")
    List<String> getAllElements();
    
    /**
     * 获取所有不重复的六亲属性
     * @return 六亲属性列表
     */
    @Query("SELECT DISTINCT six_relatives FROM yao_texts WHERE six_relatives IS NOT NULL AND six_relatives != '' ORDER BY six_relatives")
    List<String> getAllSixRelatives();
    
    /**
     * 清空所有爻辞数据 (用于重新初始化)
     */
    @Query("DELETE FROM yao_texts")
    void deleteAll();
}
